package com.ftg.elvis.mapper;

import com.ftg.elvis.pojo.Brand;
import com.ftg.elvis.pojo.Role;
import com.ftg.elvis.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface AdminUserMapper {

	/**
	 * 注册前查看用户名是否存在
	 * @param user
	 * @return
	 */
	@Select("select * from user where username = #{username}")
	User findUserByUsername(User user);

	/**
	 * 注册功能
	 * @param user
	 * @return
	 */
	@Insert("insert into user(uid, username, password, email) values(#{uid},#{username},#{password},#{email})")
	int register(User user);

	/**
	 * 登录功能
	 * @param user
	 * @return
	 */
	@Select("select * from user where username = #{username} and password = #{password}")
	User login(User user);

	@Update("update user set username = #{username}, password = #{password}, email = #{email} where uid = #{uid}")
	int updateUser(User user);

	@Delete("delete from user where uid = #{id}")
	int deleteUser(String id);

	@Select("select u.*, b.bid from user u left join user_brand b on u.username = b.username where u.username like concat(#{arg2}, '%') limit #{arg0},#{arg1}")
	List<User> showAll(int begin, int end, String username);

	@Select("select count(*) from user where username like concat(#{arg2}, '%')")
	int getCount(String username);

	/**
	 * 显示所有角色
	 * @return
	 */
	@Select("select * from role")
	List<Role> showAllRole();

	/**
	 * 显示我的所有角色
	 * @param uid
	 * @return
	 */
	@Select("select * from role r where r.rid in (select u.role_id from user_role u where u.user_id = #{uid})")
	List<Role> showMyRole(String uid);

	/**
	 * 显示我没有的角色
	 * @param uid
	 * @return
	 */
	@Select("select * from role r where r.rid not in (select u.role_id from user_role u where u.user_id = #{uid})")
	List<Role> showNoRole(String uid);

	/**
	 * 删除某个用户的所有角色
	 * @param user_id
	 * @return
	 */
	@Delete("delete from user_role where user_id = #{user_id}")
	void delMyRolesById(String user_id);

	/**
	 * 给某个用户赋角色
	 * @param role_id
	 * @param user_id
	 * @return
	 */
	@Insert("insert into user_role(role_id, user_id) values(#{arg0}, #{arg1})")
	void addMyRoles(String role_id, String user_id);

	@Select("select * from brand")
	List<Brand> showBrand();

	@Update("update user_brand set bid = #{bid} where username = #{username}")
	void updateUserBrand(User user);

	@Insert("insert into user_brand(username, bid) values(#{username}, #{bid})")
	void addUserBrand(User user);

	@Select("select * from user_brand where username = #{username} limit 1")
	User findBidByUsername(String username);

	@Delete("delete from user_role where user_id = #{id}")
	void deleteRole(String id);
}
